<%--
  Created by IntelliJ IDEA.
  User: cai
  Date: 2019/9/27
  Time: 15:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="../../common/header.jsp"%>
    <title>班级管理</title>
</head>
<body>
<div style="background-color: white">

    <div class="layui-inline" style="height: 29px">
        <label style="display: inline-block ;height: 28.6667px;font-size: 20px">班级名称查询:</label>
            <input type="text" name="className"  class="layui-input-inline" style="display: inline-block" id="classSearch">
        <label style="display: inline-block ;height: 28.6667px;font-size: 20px">班级状态查询:</label>
        <select name="classState" lay-verify="" id="classState">
            <option value="">---所 有---</option>
            <option value="0">--已开课--</option>
            <option value="1">--未开课--</option>
        </select>
    </div>
    <button class="layui-btn layui-btn-sm" style="font-family: 'Microsoft YaHei';margin-left: 50px" onclick="query_by_top(pager.pageNo)">
    <i class="layui-icon layui-icon-search"></i>
    搜索</button>
    <%--开设班级--%>
    <button type="button" class="layui-btn layui-btn-sm  layui-btn-normal" data-toggle="modal" data-target="#myModal" onclick="add_class_click()">
        <i class="layui-icon">&#xe608;</i> 开班
    </button>
    <button type="button" class="layui-btn layui-btn-sm  layui-btn-normal" data-toggle="modal" data-target="#class_Modal" onclick="query_class_room_pagers(1)">
        <i class="layui-icon layui-icon-survey"></i> 教室管理
    </button>
    <button type="button" class="layui-btn layui-btn-sm  layui-btn-normal"  data-toggle="modal" data-target="#addClassRoomModal" >
        <i class="layui-icon layui-icon-survey"></i> 添加教室
    </button>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    开设新班级
                </h4>
            </div>
            <%--表单开始--%>
            <div class="modal-body">

                <form class="form-horizontal" id="form_add_class">
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">班级名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="classname" name="classname" placeholder="请输入班级名字" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">选择校区</label>
                        <div class="col-sm-10">
                            <select  name="select_dept" id="select_dept">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">班主任</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="teacher" name="teacher" placeholder="请输入班主任名字" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">开班时间</label>
                        <div class="col-sm-10">
                            <input type="date" name="opentime" class="form-control" id="opentime" placeholder="请输入开班时间">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">结班日期</label>
                        <div class="col-sm-10">
                            <input type="date" name="endtime" class="form-control" id="endtime" placeholder="请输入结班时间">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">预招人数</label>
                        <div class="col-sm-10">
                            <input type="text" name="limitnum" class="form-control" id="limitnum" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">选择课程</label>
                        <div class="col-sm-10">
                            <select  name="select_course" id="select_course">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">选择教室</label>
                        <div class="col-sm-10">
                            <select  name="select_class_room" id="select_class_room">
                            </select>
                        </div>
                    </div>
                </form>

                <%--表单结束--%>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" onclick="add_class_up()" data-toggle="modal" data-target="#myModal">
                    添加
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<%--模态框结束--%>

<div id="showClassRoom">
<table class="layui-table">
    <tr>
        <td>教室名</td>
        <td>操作</td>
    </tr>
    <tbody id="class_tbdata"></tbody>
</table>
    <div>
        <li><span id="showClassPage" class="layui-bg-gray"></span>
            <a href="javascript:void(0)" onclick="query_class_room_pagers(1)" class="layui-btn layui-btn-sm">首页</a>
            <a href="javascript:void(0)" onclick="query_class_room_pagers(classRoomPager.prev)" class="layui-btn layui-btn-sm"><i class="layui-icon layui-icon-prev"></i></a>
            <a href="javascript:void(0)" onclick="query_class_room_pagers(classRoomPager.next)" class="layui-btn layui-btn-sm"><i class="layui-icon layui-icon-next"></i></a>
            <a href="javascript:void(0)" onclick="query_class_room_pagers(classRoomPager.pages)" class="layui-btn layui-btn-sm">末页</a>

            <input type="number" name="curPage" max="${classRoomPager.last}" min="1" step="1" id="txtCurPage2"  style="height:30px" />
            <input type="button" class="button layui-btn layui-btn-sm layui-btn-normal" value="Go" id="btn3"/>
        </li>
    </div>

</div>







<div id="showClass">
<table class="layui-table lay-even" style="margin-top: 0px" >
    <tr>
        <th>课程名</th>
        <th>班级名</th>
        <th>所在校区</th>
        <th>班级状态</th>
        <th>实招/预招</th>
        <th>开课时间</th>
        <th>结束时间</th>
        <th>教室</th>
        <th>负责教师</th>
        <th>操作</th>
    </tr>
<tbody  id="tbdata">
</tbody>
</table>
<div>
    <li><span id="showPage" class="layui-bg-gray"></span>
    <a href="javascript:void(0)" onclick="query_by_top(1)" class="layui-btn layui-btn-sm">首页</a>
    <a href="javascript:void(0)" onclick="query_by_top(pager.prev)" class="layui-btn layui-btn-sm"><i class="layui-icon layui-icon-prev"></i></a>
    <a href="javascript:void(0)" onclick="query_by_top(pager.next)" class="layui-btn layui-btn-sm"><i class="layui-icon layui-icon-next"></i></a>
    <a href="javascript:void(0)" onclick="query_by_top(pager.pages)" class="layui-btn layui-btn-sm">末页</a>

        <input type="number" name="curPage" max="${pager.last}" min="1" step="1" id="txtCurPage"  style="height:30px" />
        <input type="button" class="button layui-btn layui-btn-sm layui-btn-normal" value="Go" id="btn2"/>
    </li>
</div>
</div>
<%--录入学生模态框--%>
<%--模态框开始--%>
<!-- 模态框（Modal） -->
<div class="modal fade" id="add_student_Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="ModalLabel">分配学生</h4>
            </div>
            <div class="modal-body">

                <table class="layui-table">
                    <tr>
                        <td>
                            <label for="checkAll">全选</label>
                            <input type="checkbox" id="checkAll" />
                        </td>
                        <td>
                            未分配班级的学生
                        </td>
                        <td>
                            即将分配到的班级
                        </td>
                    </tr>
                    <tbody id="tb_without_class">

                    </tbody>
                </table>
                <input type="hidden" id="yuzhao"/>
                <input type="hidden" id="shizhao"/>
                <input type="hidden" id="shengyu"/>
                <input type="hidden" id="banji_id"/>
                <input type="hidden" id="banji_name"/>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="add_student" data-toggle="modal" data-target="#add_student_Modal">提交分配</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<%--模态框结束--%>

<%--
<button data-toggle="modal" data-target="#add_student_Modal" onclick="show_modal()">点击加载模态框</button>
--%>



<!-- 添加教室的模态框 -->
<div class="modal fade" id="addClassRoomModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="addClassRoomModalLabel">
                    添加教室
                </h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="addClassRoomForm">
                    <table>
                        <div class="form-group">
                            <div class="col-sm-10">
                                <tr>
                                    <td>教室名字:&nbsp;&nbsp;&nbsp;</td>
                                    <td>
                                        <input type="text" name="room_name" id="room_name" class="form-control">
                                    </td>
                                    <td><span id="roomNameIsExits"></span></td>
                                </tr>
                            </div>
                        </div>
                    </table>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="addClassBtn" onclick="addClassRoom()" data-dismiss="modal">
                    添加
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<%--修改班级信息--%>
<div class="modal fade" id="updateClassModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="updateClassModalLabel">
                    修改班级信息
                </h4>
            </div>
            <%--表单开始--%>
            <div class="modal-body">

                <form class="form-horizontal" id="form_update_class">
                    <input type="hidden"  id="classid2" name="classid"/>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">班级名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="classname2" name="classname" placeholder="请输入班级名字" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">选择校区</label>
                        <div class="col-sm-10">
                            <select  name="select_dept" id="select_dept2">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">班主任</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="teacher2" name="teacher" placeholder="请输入班主任名字" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">开班时间</label>
                        <div class="col-sm-10">
                            <input type="date" name="opentime" class="form-control" id="opentime2" placeholder="请输入开班时间">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">结班日期</label>
                        <div class="col-sm-10">
                            <input type="date" name="endtime" class="form-control" id="endtime2" placeholder="请输入结班时间">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">预招人数</label>
                        <div class="col-sm-10">
                            <input type="text" name="limitnum" class="form-control" id="limitnum2" required οnkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">选择课程</label>
                        <div class="col-sm-10">
                            <select  name="select_course" id="select_course2">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">选择教室</label>
                        <div class="col-sm-10">
                            <select  name="select_class_room" id="select_class_room2">
                            </select>
                        </div>
                    </div>
                </form>

                <%--表单结束--%>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" onclick="update_class_up()" data-toggle="modal" data-target="#updateClassModal">
                    修改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<%--模态框结束--%>

</body>
<script>
    var classRoomPager;
    var pager;
    var pageNum=1;
    var banji;
    var layer;

    $(function () {
        layui.use('layer', function(){
            layer = layui.layer;
        });
    })

//教室名重复校验
    $("#room_name").mouseleave(function () {
        if($("#room_name").val()==""){
            $("#roomNameIsExits").empty();
            $("#roomNameIsExits").html("未输入教室名");
            $("#addClassBtn").attr("disabled", true);
            return;
        }
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=checkRoomNameIsExits",
            data:$("#addClassRoomForm").serialize(),
            success:function (msg) {
                if(msg=="true"){
                    $("#roomNameIsExits").empty();
                    $("#roomNameIsExits").html("该教室名可以使用");
                    $("#addClassBtn").attr("disabled", false);
                } else if(msg=="false"){
                    $("#roomNameIsExits").empty();
                    $("#roomNameIsExits").html("已存在的教室名,请重新输入");
                    $("#addClassBtn").attr("disabled", true);
                }
            }
        });
    });
//添加教室执行操作
    function addClassRoom() {
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=addClassRoom",
            data:$("#addClassRoomForm").serialize(),
            success:function (msg) {
                if(msg=="ok"){
                    layer.msg("添加成功");
                    query_class_room_pagers(1);
                }else if(msg="error"){
                    layer.msg("添加失败");
                }
            },
            error:function (xhr,status,e) {
                layer.msg("错误"+e);
            }
        });
    }

    function query_class_room_pagers(curPage){
        $("#showClass").hide();
        $("#showClassRoom").show();
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=query_class_room_pagers&curPage="+curPage,
            dataType: "json",
            success:function (msg) {
                classRoomPager=msg;
                $("#class_tbdata").empty();
                var list=msg.data;
                for (let i = 0; i <list.length ; i++) {
                    if(list[i].is_use==1){
                        list[i].is_use="已占用";
                    }else{
                        list[i].is_use="未占用";
                    }
                    var tr="<tr>"+
                        "<td>"+list[i].room_name+"</td>"+
                        "<td>"+list[i].is_use+"</td>"+
                        "<td>"+"<input type='button' class='btn btn-default' value='编 辑' onclick='updateClassRoom(\"{classRoomName}\")'>"+"<input type='button' class='btn btn-danger' value='删除' onclick='deleteClassRoom(\"{classRoomName}\")'>"+"</td>"+
                        "</tr>";
                    tr=tr.replace("{classRoomName}",list[i].room_name).replace("{classRoomName}",list[i].room_name);
                    $("#class_tbdata").append(tr);
                }
                var str="当前页:"+classRoomPager.pageNo+"/总页数:"+classRoomPager.pages;
                $("#showClassPage").html(str);
                $("#txtCurPage2").val(classRoomPager.pageNo);
            }
        });
    }
    //修改教室信息
    function updateClassRoom(ClassRoomName) {
        layer.prompt({
            formType: 2,
            value: ClassRoomName,
            title: '请输入修改后的名字',
            area: ['250px', '45px'] //自定义文本域宽高
        }, function(value, index, elem){
           // layer.msg(value); //得到value
            $.ajax({
                type:"post",
                url:"${pageContext.request.contextPath}/class.action?method=update_class_room&roomName="+value+"&room_name="+ClassRoomName,
                success:function (msg) {
                    if(msg=="same"){
                        layer.msg("和原名字一样无需修改！");
                    }
                    if(msg=="isExist"){
                        layer.msg("存在形同的名字请重新输入");
                    }
                    if(msg=="ok"){
                       layer.msg("修改成功");
                        query_class_room_pagers(1);
                    }else if(msg=="error"){
                        layer.msg("修改失败");
                    }
                },
                error:function (xhr,status,e) {
                    layer.msg("错误"+e);
                }
            });
            layer.close(index);
        });
    }

    //删除教室
    function deleteClassRoom(classRoomName) {

        layer.confirm('确定删除?', function(index){
            $.ajax({
                type:"post",
                url:"${pageContext.request.contextPath}/class.action?method=deleteClassRoom&roomName="+classRoomName,

                success:function (msg) {
                    if(msg=="ok"){
                        layer.msg("删除成功");
                        query_class_room_pagers(1);
                    }else{
                        layer.msg("删除失败");
                    }
                },
                error:function (xhr,status,e) {
                    layer.msg('错误'+e);
                }
            });
            layer.close(index);
        });

    }

    function query_by_top(pageNum){
      pageNum=pageNum;
         $("#showClass").show();
        $("#showClassRoom").hide();
        var searchInfo=
            {
                className:$("#classSearch").val(),
                classState:$("#classState").val()
            };
        $.ajax({
           type:"POST",
           url:"${pageContext.request.contextPath}/class.action?method=query_by_top&pageNo="+pageNum,
           data:searchInfo,
           datatype:"JSON",
           success:function (msg) {
               var msg=eval("("+msg+")");
               pager=msg;
               $("#tbdata").empty();
               //layer.msg(msg.data[0].classname);
               var list=msg.data;
               for (var i = 0; i <list.length ; i++) {
                   if(list[i].progressing==0){
                       list[i].progressing="开班";
                   }else{
                       list[i].progressing="结班";
                   }
                   var tr;
                       tr="<tr>"+
                       "<td>"+list[i].course_name+"</td>"+
                       "<td>"+list[i].classname+"</td>"+
                       "<td>"+list[i].dept_name+"</td>"+
                       "<td>"+list[i].progressing+"</td>"+
                       "<td>"+list[i].acnumber+"/"+list[i].limitnum+"</td>"+
                       "<td>"+list[i].opentime+"</td>"+
                       "<td>"+list[i].endtime+"</td>"+
                       "<td>"+list[i].classroom+"</td>"+
                       "<td>"+list[i].teacher+"</td>"+

                       "<td>"+"<select name=\"classState\" lay-verify=\"\" id=\"classState\">\n" +
                       "            <option value=\"\" selected=\"selected\">---操 作---</option>\n" +
                       "            <option onclick=\"update_class({class_id})\" data-toggle=\"modal\" data-target=\"#updateClassModal\">---编 辑---</option>\n" +
                       "            <option onclick=\"open_close({class_id})\">--开班/结班--</option>\n" +
                       "            <option onclick=\"student_without_class(\'{class_id}\',\'{class_name}\')\" data-toggle=\"modal\" data-target=\"#add_student_Modal\">--分配学员--</option>\n" +
                         "        <option onclick=\"delete_class({class_id})\">---删 除---</option>\n" +
                       "            </select>"+
                       "</tr>";
                   tr=tr.replace("{class_id}",list[i].classid).replace("{class_id}",list[i].classid).
                   replace("{class_id}",list[i].classid).replace("{class_name}",list[i].classname).replace("{class_id}",list[i].classid);
                   $("#tbdata").append(tr);
               }
               var str="当前页:"+pager.pageNo+"/总页数:"+pager.pages;
               $("#showPage").html(str);
               $("#txtCurPage").val(pager.pageNo);
           },
            error:function (xhr,status,e) {
                layer.msg("错误"+e);
            }
        });
    }
    $("#btn2").click(function () {
        var into=$("#txtCurPage").val();
        if(into>pager.pages){
            $("#txtCurPage").val(pager.pages);
        }
        if(into<=1){
            $("#txtCurPage").val(1);
        }
        query_by_top($("#txtCurPage").val());
    });

    $("#btn3").click(function () {
        var into=$("#txtCurPage2").val();
        if(into>classRoomPager.pages){
            $("#txtCurPage2").val(classRoomPager.pages);
        }
        if(into<=1){
            $("#txtCurPage2").val(1);
        }
        query_class_room_pagers($("#txtCurPage2").val());
    });

    //删除班级
    function delete_class(classid) {
      var isDelete=confirm("确定删除？");
      if(!isDelete){
          return;
      }
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=delete_class&classid="+classid,
            success:function (msg) {
                if(msg=="ok"){
                    layer.msg("删除成功");
                    query_by_top(1);
                }else{
                    layer.msg("删除失败");
                }
            },
            error:function (xhr,status,e) {
                layer.msg("错误"+info+e);
            }
        });
    }
     function add_class_click(){
            //
             $.ajax({
                 type:"post",
                 url:"${pageContext.request.contextPath}/class.action?method=select_dept",
                 datatype:"json",
                 success:function (dept_msg) {
                     $("#select_dept").empty();
                     var list=eval("("+dept_msg+")");
                     //var list=dept_msg.data;
                     //layer.msg(list.length);
                     $("#select_dept").append("<option value=''>-请选择-</option>");
                     for (var i = 0; i < list.length; i++) {
                         /*value='"+list[i].dept_id+"'*/
                         var res="<option value='"+list[i].dept_name+"'>"+list[i].dept_name+"</option>";
                             $("#select_dept").append(res);
                     }
                 },
                 error:function (xhr,status,e) {
                     layer.msg("错误"+e);
                 }
             });

             //课程下拉列表
         $.ajax({
             type:"post",
             url:"${pageContext.request.contextPath}/class.action?method=select_course",
             datatype:"json",
             success:function (couse_msg) {
                 $("#select_course").empty();
                 var list=eval("("+couse_msg+")");
                 $("#select_course").append("<option value='0'>-请选择-</option>");
                 for (var i = 0; i < list.length; i++) {
                     var res="<option value='"+list[i].course_id+"'>"+list[i].course_name+"</option>";
                     $("#select_course").append(res);
                 }
             },
             error:function (xhr,status,e) {
                 layer.msg("错误"+e);
             }
         });

         //教室下拉列表
         $.ajax({
             type:"post",
             url:"${pageContext.request.contextPath}/class.action?method=select_class_room",
             datatype:"json",
             success:function (class_room_msg) {
                 $("#select_class_room").empty();
                 var list=eval("("+class_room_msg+")");
                 $("#select_class_room").append("<option value=''>-请选择-</option>");
                 for (var i = 0; i < list.length; i++) {
                     var res="<option value='"+list[i].room_name+"'>"+list[i].room_name+"</option>";
                     $("#select_class_room").append(res);
                 }
             },
             error:function (xhr,status,e) {
                 layer.msg("错误"+e);
             }
         });

         //设置默认时间
         $(document).ready(function () {
             var time = new Date();
             var day = ("0" + time.getDate()).slice(-2);
             var month = ("0" + (time.getMonth() + 1)).slice(-2);
             var today = time.getFullYear() + "-" + (month) + "-" + (day);
             $('#opentime').val(today);
             $('#endtime').val(today);

         });
     }

     //添加班级后的提交操作
      function add_class_up(){
         if($("#classname").val()==""||$("#select_dept").val()==""||$("#teacher").val()==""||$("#opentime").val()==""||
             $("#endtime").val()==""||$("#limitnum").val()==""||$("#select_course").val()==""||$("#select_class_room").val()==""){
             layer.msg("不能有空内容");
             return 0;
         }
         $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=add_class_up",
            data:$("#form_add_class").serialize(),
             success:function (msg) {
                 if(msg=="ok"){
                     layer.msg("开班成功!");
                     query_by_top(1);
                 }
                 if(msg=="error"){
                     layer.msg("开班失败!");
                 }
             },
             error:function (xhr,status,e) {
                 layer.msg("错误"+e);
             }
         });
      }

    //下拉菜单操作
    this.$("#classState").change(function () {
        var result=this.$("#classState").val().split(",");
        layer.msg(result[0]);
        layer.msg(result[1]);
    });

    //把原来的班级信息存起来，方便下面取班级
    var classInfo;
//修改班级显示原来的班级信息
    function update_class(classid){
        $("#classid2").val(classid);
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=select_dept",
            datatype:"json",
            success:function (dept_msg) {
                $("#select_dept2").empty();
                var list=eval("("+dept_msg+")");
                //var list=dept_msg.data;
                //layer.msg(list.length);
                $("#select_dept2").append("<option value=''>-请选择-</option>");
                for (var i = 0; i < list.length; i++) {
                    /*value='"+list[i].dept_id+"'*/
                    var res="<option value='"+list[i].dept_name+"'>"+list[i].dept_name+"</option>";
                    $("#select_dept2").append(res);
                }
            },
            error:function (xhr,status,e) {
                layer.msg("错误"+e);
            }
        });

        //课程下拉列表
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=select_course",
            datatype:"json",
            success:function (couse_msg) {
                $("#select_course2").empty();
                var list=eval("("+couse_msg+")");
                $("#select_course2").append("<option value='0'>-请选择-</option>");
                for (var i = 0; i < list.length; i++) {
                    var res="<option value='"+list[i].course_id+"'>"+list[i].course_name+"</option>";
                    $("#select_course2").append(res);
                }
            },
            error:function (xhr,status,e) {
                layer.msg("错误"+e);
            }
        });


        //绑定原始数据
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=queryOneClass&classid="+classid,
            dataType:"json",
            success:function (msg) {
                classInfo=msg;
                //var list=eval("("+msg+")");
                $("#classid2").val(classid);
                $("#classname2").val(msg[0].classname);
                $("#teacher2").val(msg[0].teacher);
                $("#limitnum2").val(msg[0].limitnum);
            },
            error:function (xhr,status,e) {
                layer.msg("错误"+e);
            }
        });




        //教室下拉列表
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=select_class_room",
            dataType:"json",
            success:function (class_room_msg) {
                $("#select_class_room2").empty();
                var list=eval("("+class_room_msg+")");
                $("#select_class_room2").append("<option value=''>-请选择-</option>");
                for (var i = 0; i < list.length; i++) {
                    var res="<option value='"+list[i].room_name+"'>"+list[i].room_name+"</option>";
                    $("#select_class_room2").append(res);
                }
                //添加原来的教室到下拉选项
                if(classInfo[0].progressing==0){
                    var preClassInfo="<option value='"+classInfo[0].classroom+"'>"+classInfo[0].classroom+"</option>";
                    $("#select_class_room2").append(preClassInfo);
                }
            },
            error:function (xhr,status,e) {
                layer.msg("错误"+e);
            }
        });

        //设置默认时间
        $(function () {
            var time = new Date();
            var day = ("0" + time.getDate()).slice(-2);
            var month = ("0" + (time.getMonth() + 1)).slice(-2);
            var today = time.getFullYear() + "-" + (month) + "-" + (day);
            $('#opentime2').val(today);
            $('#endtime2').val(today);
        });



    }

    //修改班级信息的提交操作
    function update_class_up(){
        if($("#classname2").val()==""||$("#select_dept2").val()==""||$("#teacher2").val()==""||$("#opentime2").val()==""||
            $("#endtime2").val()==""||$("#limitnum2").val()==""||$("#select_course2").val()==""||$("#select_class_room2").val()==""){
            layer.msg("不能有空内容");
            return 0;
        }
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=add_class_up",
            data:$("#form_update_class").serialize(),
            success:function (msg) {
                if(msg=="ok"){
                    layer.msg("修改成功");
                    query_by_top(1);
                }else{
                    layer.msg("修改失败");
                }
            },
            error:function (xhr,status,e) {
                layer.msg("错误"+e);
            }
        });
    }

    //下拉菜单之分班操作
    function student_without_class(classid,classname) {
       // layer.msg(classid);
        //layer.msg(classname);
        banji=classname;
        //查询要被分配学员的班级信息
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=query_class_one&classid="+classid,
            dataType:"JSON",
            success:function (msg) {
                //var msg=eval("("+msg+")");
                //layer.msg(msg.courseid);
                $("#yuzhao").val(msg.limitnum);
                $("#shizhao").val(msg.acnumber);
                $("#banji_id").val(msg.classid);
                $("#banji_name").val(msg.classname);

                var sheng=msg.limitnum-msg.acnumber;
              //  layer.msg(sheng);
                $("#shengyu").val(sheng);
            }
        });
        //查询未分班的学员异步请求
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=query_without_class",
            datatype:"json",
            success:function (msg) {
                $("#tb_without_class").empty();
                var list2=eval("("+msg+")");
                // var list=msg.data;
                if(list2.length<=0){
                    $("#tb_without_class").append("<tr><td colspan='3' style='text-align: center'>没有未分班的学员</td></tr>");
                    return;
                }
                for (var i = 0; i <list2.length ; i++) {
                    var tr="<tr>" +
                        "<td><input type='checkbox' class='item' value='{stuid}'>"+
                        "<td>"+list2[i].stuname+"</td>"+
                        "<td>"+banji+"</td>"+
                    "</tr>";
                    tr=tr.replace("{stuid}",list2[i].stuid);
                    $("#tb_without_class").append(tr);
                }
            }
        });
    }


    //全选
    $("#checkAll").click(function () {
        //设置同一类型的复选框的选中属性跟 id为checkAll 的保持一致
        $(".item").prop("checked",$(this).prop("checked"));
    });

    $("#add_student").click(
        function(){
            //获取要提交的数据
            var lenth= $(":checked").size();
            if(lenth<=0){
                layer.msg("请先选择数据");
                return;
            }

            //准备一个数组用来存放
            var ids=[];
            $(".item:checked").each(function(i,e){
                ids.push(e.value);
            });
            //要提交给后台的数据,需要转换成字符串
            var strIds= ids.join(",");

           // layer.msg("要提交的数据，后台 需要split成数组："+strIds);


            if(ids.length==0){
                layer.msg("未为选择学员，请重新选择！");
                return;
            }

            //告诉用户录入的条数，该班预招人数，已招人数。
           var isYes=confirm("您将要为"+$("#banji_name").val()+"班添加"+ids.length+"个学生,该班预招人数:"+$("#yuzhao").val()+"人，已招:"+$("#shizhao").val()+"人,距招满还差:"+$("#shengyu").val()+"人");
            if(!isYes){
                return;
            }
            //异步请求
            $.ajax({
                type:"post",
                data:{"strIds":strIds},
                url:"${pageContext.request.contextPath}/class.action?method=update_student_classid&classid="+$("#banji_id").val(),
                success:function (msg) {
                    if(parseInt(msg)==ids.length){
                        layer.msg("录入成功！"+$("#banji_name").val()+"班共录入:"+msg+"个学生");
                        query_by_top(1);
                    }
                },
                error:function (xhr,status,e) {
                    layer.msg("录入错误"+e);
                }
            });
        }
    );
//开课，结课
    function open_close(classid){
        $.ajax({
            type:"post",
            url:"${pageContext.request.contextPath}/class.action?method=update_class_progressing&classid="+classid,
            success:function (result) {
                if(result=="ok"){
                    query_by_top(1);
                }else{
                    layer.msg("修改状态失败！");
                }
            },
            error:function (xhr,status,e) {
                layer.msg("修改开班/结班错误"+e);
            }
        });

    }

    $(function () {
        query_by_top(1);
    });

</script>
</html>
